// pages/home/home.js
import {
  request
} from './api';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    background: [],
    bannerlist: [],
    vertical: false,
    autoplay: true,
    indicatordots: true,
    interval: 5000,
    duration: 500,
    dataList: [], //商品列表总数
    // token数据，个人信息
    openid: "",
    nick_name: '',
    icon: "",
    avatarUrl: "",
    userInfo: "",
    person_info: "",
    shoplist: [], //分页显示商品列表
    currPage: 1,
    pagesize: 4,
    tag: "",
    countnum: 0, //总的商品列表数量
    loading: true,
    setting_status: 2,
    selected:0
  },

  GetUserEntity: function () {
    return new Promise(function (resolve, reject) {
      wx.request({
        url: "http://api_dev.wanxikeji.cn/api/setting",
        data: {
          token: wx.getStorageSync('token')
        },
        success: (res) => {
          let result = res;
          resolve(result);
        },
        fail: () => {
          reject("系统异常，请重试！")
        }
      })
    })

  },
  // 接口
  getOpenid: function () {
    let that = this;
    wx.login({
      success: function (res) {
        wx.request({
          method: "POST",
          url: "http://api_dev.wanxikeji.cn/api/codeExchangeOpenid",
          data: {
            code: res.code
          },
          success: function (response) {
            console.log(response)
            var openid = response.data.data.openid;
            // openid存缓存
            wx.setStorageSync('openid', openid);

            // token存缓存

            // 个人信息存缓存
            if (response.data.data.info) {
              var token = response.data.data.info.token;
              console.log(token)
              wx.setStorageSync('token', token);
              wx.setStorageSync('person_info', response.data.data.info);
              that.setData({
                icon: response.data.data.info.icon,
                nick_name: response.data.data.info.nick_name,
                hasUserInfo: true
              })
            }
            that.setData({
              openid: openid,
            })
          },
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
   
    // 登录获取token数据
    this.getOpenid();
    var _this = this
    //获取授权状态
    if (!wx.getStorageSync('setting_status')) {
      this.GetUserEntity().then((res) => {
        if (res.data.code == 2000) {
          _this.setData({
            setting_status: res.data.data[0].status
          })
          wx.setStorageSync('setting_status', this.data.setting_status);
        }
      }).catch((res) => {
        console.log(res)
      })
    } else {
      _this.setData({
        setting_status: wx.getStorageSync('setting_status')
      })
    }

    

    // 商品展示接口
    request({
        url: '/goodList',
      })
      .then((el) => {
        _this.setData({
          countnum: el.data.data.count
        })
        request({
          url: '/goodList',
          data: {
            is_new: 1,
            page: 1,
            size: _this.data.countnum
          }
        }).then((res) => {
          var dataList = []
          res.data.data.data.forEach(item => {
            dataList.push({
              imgUrl: item.img,
              title: item.good_name,
              price: item.price,
              good_id: item.good_id
            })
          })
          _this.setData({
            dataList,
            loading: false
          })
          _this.setData({
            shoplist: _this.data.dataList.slice(0, _this.data.pagesize * _this.data.currPage)
          })
       
        })

      })

    //轮播图接口
    request({
        url: '/bannerList'
      })
      .then((photo) => {
        var bannerlist = []
        photo.data.data.forEach(item => {
          bannerlist.push(
            item.pic,
          )
        });
        _this.setData({
          bannerlist,
          loading: false
        })
      })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

    //  wx.showNavigationBarLoading() //在标题栏中显示加载
    // 刷新并初始化数据

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let _this = this
    if(this.data.dataList.length>0){
      if (this.data.currPage * this.data.pagesize > this.data.dataList.length) {
        setTimeout(function () {
          _this.setData({
            tag: "没有更多了"
          })
        }, 1000)
        setTimeout(function () {
          _this.setData({
            tag: ""
          })
        }, 3000)
      } else {
        setTimeout(function () {
          _this.setData({
            tag: "正在加载中……"
          })
        }, 1000)
        _this.setData({
          currPage: ++_this.data.currPage,
          shoplist: _this.data.dataList.slice(0, _this.data.pagesize * _this.data.currPage)
        })
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
